﻿using System;
using System.Collections.Generic;
using System.Text;
using PPI.Entities.Distribution;
using PPI.Mobile.Logic;
using System.Data;
using VMind.Global;

namespace PPI.Client.Logic.Distribution
{
    public class LPhase
    {
        public static List<EPhase> SelectAllIdName()
        {
            DataTable dt = WebConsumer.Distribution.WDistributionPolePhase.SelectAllIdName().Tables[0];
            List<EPhase> lstPhases = new List<EPhase>();

            foreach (DataRow drTop in dt.Rows)
            {
                EPhase phase = new EPhase();

                phase.Id = ConvertHelper.ToInt32(drTop["PhaseId"]);
                phase.Name = drTop["Name"].ToString();

                lstPhases.Add(phase);
            }

            return lstPhases;            
        }

        public static List<EPhase> Save(List<EPhase> phases, Int32 poleId)
        {
            return WebConsumer.Distribution.WDistributionPolePhase.Save(phases, poleId);
        }


        public static void UpdateUploadedData(List<EPhase> phases, Int32 poleId, string connectionString)
        {
            foreach (EPhase phase in phases)
            {
                StringBuilder _query = new StringBuilder();
                _query.AppendLine("UPDATE tb_PhasesXPoles set PhaseXPoleServerID = " + phase.ServerId + ", PoleServerID = " + poleId + ", UploadedDate = " +
                                  DateTime.Now.ToString("yyyy-MM-dd") + " where PhaseXPoleId =" + phase.Id + " ");

                LMobileDatabase.ExecuteQuery(_query.ToString(), connectionString);
            }
        }

        public static List<EPhase> GetPolePhases(EDistributionPole distributionPole, string connectionString)
        {
            StringBuilder _sql = new StringBuilder();

            #region Query

            _sql.AppendLine("SELECT ");
            _sql.AppendLine("   PP.PhaseXPoleId, ");
            _sql.AppendLine("   PP.PhaseFk, ");
            _sql.AppendLine("   LKS.LookupText ");
            _sql.AppendLine("FROM tb_PhasesXPoles PP ");
            _sql.AppendLine("LEFT JOIN tb_Lookups LKS ON LKS.LookupValue = PP.PhaseFk AND LKS.LookupTypeFk = 'Phase' ");
            _sql.AppendLine("WHERE PP.PoleFk = " + distributionPole.Id + ";");

            #endregion

            DataTable dt = LMobileDatabase.ExecuteQueryDataset(_sql.ToString(), connectionString).Tables[0];

            List<EPhase> lstPhases = new List<EPhase>();

            foreach (DataRow dr in dt.Rows)
            {
                EPhase polePhase = new EPhase();

                polePhase.Key = dr["PhaseFk"].ToString();
                polePhase.Id = ConvertHelper.ToInt32(dr["PhaseXPoleId"]);
                polePhase.Name = dr["LookupText"].ToString();

                lstPhases.Add(polePhase);
            }

            return lstPhases;
        }

        public static List<EPhase> GetProjectPolePhases(EDistributionPole distributionPole, string connectionString)
        {
            StringBuilder _sql = new StringBuilder();

            #region Query

            _sql.AppendLine("SELECT ");
            _sql.AppendLine("   PP.PhaseXPoleId, ");
            _sql.AppendLine("   PP.PhaseFk, ");
            _sql.AppendLine("   LKS.LookupText ");
            _sql.AppendLine("FROM tb_PhasesXPoles PP ");
            _sql.AppendLine("LEFT JOIN tb_Lookups LKS ON LKS.LookupValue = PP.PhaseFk AND LKS.LookupTypeFk = 'Phase' ");
            _sql.AppendLine("WHERE PP.PoleFk = " + distributionPole.Id + ";");

            #endregion

            DataTable dt = LMobileDatabase.ExecuteQueryDataset(_sql.ToString(), connectionString).Tables[0];

            List<EPhase> lstPhases = new List<EPhase>();

            foreach (DataRow dr in dt.Rows)
            {
                EPhase polePhase = new EPhase();

                polePhase.Key = dr["PhaseFk"].ToString();
                polePhase.Id = ConvertHelper.ToInt32(dr["PhaseXPoleId"]);
                polePhase.Name = dr["LookupText"].ToString();
                
                lstPhases.Add(polePhase);
            }

            return lstPhases;
        }
    }
}
